
package cz.geoserver.KMLpublisher.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cz.geoserver.KMLpublisher.db.SQLExecutor;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * This servlet is not used this time.
 * @author mkepka
 *
 */
public class KMLServlet extends HttpServlet {

    /** serialVersionUID */
    private static final long serialVersionUID = 1L;
    private static final String propertiesFile = "WEB-INF/database.properties";
    private static String tempName = "pointKML.ftl";
    private static Template pointKML = null;
    NumberFormat formatter = new DecimalFormat("#0.00000");
    
    /**
     * 
     */
    public void init() throws ServletException {
        super.init();
        String propFile = getServletContext().getRealPath(propertiesFile);
        Properties prop = new Properties();
        try {
        	prop.load(new FileInputStream(propFile));
        	SQLExecutor.setProperties(prop);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        
        Configuration cfg = new Configuration();
        try {
        	String resourcePathName = getClass().getResource(tempName).getFile();
        	String resourceDirName = resourcePathName.replace(tempName, "");
        	File templateDir = new File(resourceDirName);
        	
        	cfg.setDirectoryForTemplateLoading(templateDir);
        	cfg.setObjectWrapper(new DefaultObjectWrapper());
        	pointKML = cfg.getTemplate(tempName);
        } catch (IOException e) {
        	e.printStackTrace();
        }
    }
    
    
    /**
     * 
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        //super.doPost(req, resp);
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        //super.doGet(req, resp);
        Map parMap = req.getParameterMap();
        String parX = "x";
        String parY = "y";
        
        if(parMap.containsKey(parX) && parMap.containsKey(parY)){
            resp.setContentType("application/vnd.google-earth.kml+xml");
            resp.setStatus(200);
            PrintWriter pw = resp.getWriter();
         // GET example http://localhost:8181/publisher/KMLServlet?x=15&y=50
            Map<String, String> map = new HashMap<String, String>();
            map.put("placemarkName", "Position");
            map.put("placemarkDescription", "Unit Position");
            map.put("iconColor", "ff85befd");
            map.put("x", req.getParameter(parX));
            map.put("y", req.getParameter(parY));
            try {
    			pointKML.process(map, pw);
    		} catch (TemplateException e) {
    			e.printStackTrace();
    		}
        }
    }
}
